var folderEX = require(path.join(__dirname, '_list.js')).folder;
var dialogEX = require(path.join(__dirname, '_dialog.js'));

// var dialog = dialogEX({});
var chooseObj = function (activeData) {
    this.activeData = activeData;
    this.html = $('<div class="chooseFolder list"></div>');
    this.items = {};

    this.root = new folderEX({
        id: null,
        contentType: 'folder',
        // parentId: null,
        title: '/root',
        contentIndex: ''
    }, this);
    this.items[null] = this.root;
    this.html.append(this.root.render());

    this.root.icon.css('display', 'none');

    this.root.name.css('left', '0px');
    this.root.text.css('left', '0px');

    this.root.title.off('mousedown');

    this.root.title.click(() => {
        if (this.root.config.parentId !== undefined) {
            if (this.root.status) {
                this.root.close();
            } else {
                this.root.open();
            }
        };
        this.root.parent.active(this.root.config.id);
    });
    this.activeObj = null;
    this.event = {
        active: function (e) { },
        shallMenu: function (e) { },
        rename: function (e) { },
        newFolder: function (e) { },
        delete: function (e) { },
        chooseEnd: function () { }
    };
};

chooseObj.prototype = {
    constructor: this,
    render: function (_array, closeCallback) {
        this.setData(_array);
        var dialog = dialogEX({
            title: '选择文件夹',
            body: this.html,
            buttons: [{
                text: '确定',
                event: (event) => {
                    if (!this.activeObj) {
                        dialogEX({
                            title: '警告',
                            body: '<div style="padding:20px;">请选择文件夹</div>',
                            buttons: [{
                                text: '确定',
                                event: (event) => {
                                    event.close();
                                },
                            }],
                            width: 400,
                            height: 200,
                        });
                    } else {
                        this.event.chooseEnd(event, this.activeObj.config, this.activeData);
                    }
                },
            }],
            width: 600,
            height: 500,
            closeEvent: closeCallback
        });
    },
    each: function (cb) {
        for (const key in this.items) {
            if (this.items.hasOwnProperty(key)) {
                const element = this.items[key];
                cb(element);
            }
        }
    },
    setData: function (_array) {
        for (let i = 0; i < _array.length; i++) {
            const element = _array[i];
            if (element.contentType == 'folder') {
                this.items[element.id] = new folderEX(element, this);
            } else {
                this.items[element.id] = new interface(element, this);
            }
        };
        this.each((element) => {
            if (element.config.parentId !== undefined) {
                this.items[element.config.parentId].append(element);
            };
        });
        this.each((element) => {
            if (element.config.contentType == 'folder') {
                element.sort();
            };
        });
        this.each((element) => {
            element.parentLevel();
        });
        if (this.activeData) {
            if (this.activeData.contentType == 'folder') {
                this.items[this.activeData.id].title.off('click');
                this.items[this.activeData.id].title.css({
                    'background': '#35ce00',
                    'color': '#fff'
                });
            } else {
                this.items[this.activeData.parentId].title.css({
                    'background': '#35ce00',
                    'color': '#fff'
                });
            };
        };
    },
    active: function (id) {
        if (this.items.hasOwnProperty(id)) {
            if (this.items[id] !== this.activeObj) {
                this.unActive();
                this.items[id].active();
                this.activeObj = this.items[id];
            };
        } else {
            this.unActive();
            this.activeObj = null;
        };
    },
    unActive: function () {
        this.each((ele) => {
            ele.unActive();
        });
    },
};


module.exports.chooseFolder = chooseObj;






